package com.laikan.reader.database.db;

import android.content.Context;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.comment.APIProtocol;
import com.socks.library.KLog;

import java.util.List;

/**
 * @author wx
 * @version 创建时间：2015-12-15 下午3:46:31
 * @E-mail wx1343571153@163.com
 */

public class HistoryTask implements Runnable {

    private List<BookRecord> recordList;
    private DAO dao;
    private Context context;

    public HistoryTask(List<BookRecord> recordList, Context context, DAO dao) {
        this.recordList = recordList;
        this.context = context;
        this.dao = dao;
    }

    @Override
    public void run() {

        // 接口地址
        String useid;
        String url = APIProtocol.getRootURL2() + "reader_history?sd=" + SPUtil.getString("token", "");
        // 请求参数
        for (int i = 0; i < recordList.size(); i++) {

            if (recordList.get(i).getUserId().equals("0")) {
                useid = SPUtil.getString("user_id", "");
            }else {
                useid = recordList.get(i).getUserId();
            }

            url += "&userIdes=" + useid;

            url += "&ides=" + recordList.get(i).getData();
            url += "&type=" + "0";
        }

        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                System.out.println(httpResponse.responseContent);
                for (int i = 0; i < recordList.size(); i++) {
                    dao.delete(recordList.get(i).getUserId(), recordList.get(i).getData());
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {

            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(url);
//        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(url, null, null, null, requestListener);
    }
}
